package com.ficv.tucanalmovil.dominio;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.OneToOne;
import javax.persistence.TypedQuery;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooJpaActiveRecord(finders = { "findAlertaAproxesByObserverAndTarget", "findAlertaAproxesByObserverAndTargetAndEstado" })
public class AlertaAprox {

    @OneToOne
    private People target;

    @OneToOne
    private People observer;

    private Integer meters;

    private Integer minutes;

    private Integer estado;

    public static TypedQuery<com.ficv.tucanalmovil.dominio.AlertaAprox> findTargetWithObserver(Double meters, Integer minutes) {
        EntityManager em = Peticiones.entityManager();
        TypedQuery<AlertaAprox> q = em.createQuery("SELECT o FROM AlertaAprox AS o  WHERE  o.target.hora > :minHora AND " + "NOT EXISTS (SELECT c FROM Contactos AS c WHERE c.blockedAtoB > 1 AND c.contactoA=o.target AND c.contactoB=o.observer  OR c.blockedBtoA > 1 AND c.contactoB=o.target AND c.contactoA=o.observer) " + "AND ( 6371 * FUNC('acos',FUNC('cos',FUNC('radians',o.target.lat) ) * FUNC('cos', FUNC('radians', o.observer.lat ) ) * FUNC('cos',FUNC('radians', o.observer.lon ) - FUNC('radians', o.target.lon) ) + FUNC('sin', FUNC('radians',o.target.lat) ) * FUNC('sin', FUNC('radians', o.observer.lat ) ) ) ) < :meters  ORDER BY o.target.hora DESC", AlertaAprox.class);
        Calendar cal = new GregorianCalendar();
        cal.setTime(new Date());
        cal.add(Calendar.MINUTE, -minutes);
        q.setParameter("minHora", cal.getTime());
        q.setParameter("meters", meters);
        return q;
    }

    public static TypedQuery<com.ficv.tucanalmovil.dominio.AlertaAprox> findTargetWithObserver() {
        EntityManager em = Peticiones.entityManager();
        TypedQuery<AlertaAprox> q = em.createQuery("SELECT o FROM AlertaAprox AS o  WHERE  o.target.hora > :minHora AND o.estado < 1 AND " + "NOT EXISTS (SELECT c FROM Contactos AS c WHERE c.blockedAtoB > 1 AND c.contactoA=o.target AND c.contactoB=o.observer  OR c.blockedBtoA > 1 AND c.contactoB=o.target AND c.contactoA=o.observer) " + "AND ( 6371 * FUNC('acos',FUNC('cos',FUNC('radians',o.target.lat) ) * FUNC('cos', FUNC('radians', o.observer.lat ) ) * FUNC('cos',FUNC('radians', o.observer.lon ) - FUNC('radians', o.target.lon) ) + FUNC('sin', FUNC('radians',o.target.lat) ) * FUNC('sin', FUNC('radians', o.observer.lat ) ) ) ) < (o.meters/1000)  ORDER BY o.target.hora DESC", AlertaAprox.class);
        Calendar cal = new GregorianCalendar();
        cal.setTime(new Date());
        System.out.println(cal + ": " + cal.getTime() + ", " + cal.getTimeZone());        
        if(cal.getTimeZone().getRawOffset()==0) cal.add(Calendar.HOUR_OF_DAY, -2);
        cal.add(Calendar.MINUTE, 5);
        System.out.println(cal + ": " + cal.getTime() + ", " + cal.getTimeZone().getRawOffset());
        q.setParameter("minHora", cal.getTime());
        return q;
    }
}
